using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Peak.Core;
using SharpArch.Core.PersistenceSupport;
using Microsoft.Practices.ServiceLocation;

namespace Peak.Web.Controllers.Services
{
  public class SessionService : ISessionService
  {
    private const string CurrentProjectId = "CurrentProjectId";
    private const string CurrentStoryId = "CurrentStoryId";
    private const string CurrentSprintId = "CurrentSprintId";
    private const string BackLinkConst = "BackLink";

    private readonly IRepository<Project> _projectRepository;
    private readonly IRepository<Story> _storyRepository;
    private readonly IRepository<Sprint> _sprintRepository;

    public SessionService(IRepository<Project> projectRepository, IRepository<Story> storyRepository, IRepository<Sprint> sprintRepository)
    {
      _projectRepository = projectRepository;
      _storyRepository = storyRepository;
      _sprintRepository = sprintRepository;
    }

    public int ProjectId
    {
      get
      {
        if(System.Web.HttpContext.Current.Session[CurrentProjectId] == null)
          throw new ProjectNotSelectedException();
        return (int)System.Web.HttpContext.Current.Session[CurrentProjectId];
      }
      set
      {
        System.Web.HttpContext.Current.Session[CurrentProjectId] = value;
      }
    }

    public int StoryId
    {
      get
      {
        if(System.Web.HttpContext.Current.Session[CurrentStoryId] == null)
          throw new StoryNotSelectedException();
        return (int)System.Web.HttpContext.Current.Session[CurrentStoryId];
      }
      set
      {
        System.Web.HttpContext.Current.Session[CurrentStoryId] = value;
      }
    }

    public Project Project
    {
      get
      {
        return _projectRepository.Get(ProjectId);
      }
      set
      {
        if(value == null)
        {
          System.Web.HttpContext.Current.Session[CurrentProjectId] = null;
        }
        else
        {
          System.Web.HttpContext.Current.Session[CurrentProjectId] = value.Id;
        }
      }
    }

    public Story Story
    {
      get
      {
        return _storyRepository.Get(StoryId);
      }
      set
      {
        if (value == null)
        {
          System.Web.HttpContext.Current.Session[CurrentStoryId] = null;
        }
        else
        {
          System.Web.HttpContext.Current.Session[CurrentStoryId] = value.Id;
        }
      }
    }    

    public bool IsProjectSelected
    {
      get 
      {
        return System.Web.HttpContext.Current.Session[CurrentProjectId] != null;
      }
    }        

    public int SprintId
    {
      get
      {
        if (System.Web.HttpContext.Current.Session[CurrentSprintId] == null)
          throw new ProjectNotSelectedException();
        return (int)System.Web.HttpContext.Current.Session[CurrentSprintId];
      }
      set
      {
        System.Web.HttpContext.Current.Session[CurrentSprintId] = value;
      }
    }

    public Sprint Sprint
    {
      get
      {
        return _sprintRepository.Get(SprintId);
      }
      set
      {
        if (value == null)
        {
          System.Web.HttpContext.Current.Session[CurrentSprintId] = null;
        }
        else
        {
          System.Web.HttpContext.Current.Session[CurrentSprintId] = value.Id;
        }
      }
    }
        
    public BackLink BackLink
    {
      get
      {
        if (System.Web.HttpContext.Current.Session[BackLinkConst] == null)
        {
          System.Web.HttpContext.Current.Session[BackLinkConst] = new BackLink();
        }

        return (BackLink) (System.Web.HttpContext.Current.Session[BackLinkConst]);       
      }
    }

  }
}